/* 
    文章列表组件
 */
<template>
    <div class="articleList">
        <van-pull-refresh v-model="refreshingLoading" @refresh="onRefresh" :success-text="refreshingText"
            :success-duration="1500">
            <van-list v-model="loading" @load="onLoad" :finished="finished" finished-text="没有更多了" :error.sync="error"
                error-text="请求失败，点击重新加载">

                <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
                <ArticleItem v-for="(item, index) in list" :key="index" :article="item" />

            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script>
// import { getArticleList } from '@/api/control/control.js'
import ArticleItem from "@/components/articleItem/index"

export default {
    name: 'ArticleList',
    props: {
        channel: {
            type: Object,
            required: true
        }
    },
    components: {
        ArticleItem
    },
    data() {
        return {
            list: [
                { id: 1, title: 'html11111111111111111', author: '老王', count: 1, time: "2022-01-13", imageList: ['https://img01.yzcdn.cn/vant/cat.jpeg'] },
                { id: 2, title: 'css', author: '老李', count: 2, time: "2022-08-13", imageList: ['https://img01.yzcdn.cn/vant/cat.jpeg', 'https://img01.yzcdn.cn/vant/cat.jpeg', 'https://img01.yzcdn.cn/vant/cat.jpeg'] },
                { id: 3, title: 'js11111111111111111111111', author: '老王', count: 1, time: "2022-08-13", imageList: [] },
                { id: 4, title: 'vue', author: '赵四', count: 1, time: "2022-08-13", imageList: [] },
                { id: 5, title: 'vant', author: '老王', count: 4, time: "2022-02-13", imageList: [] },
                { id: 6, title: 'react', author: '老王', count: 1, time: "2022-08-13", imageList: ['https://img01.yzcdn.cn/vant/cat1.jpeg'] },
                { id: 7, title: 'vite', author: '老王', count: 1, time: "2022-08-13", imageList: [] },
                { id: 8, title: 'vite', author: '老王', count: 5, time: "2022-05-13", imageList: [] },
                { id: 9, title: 'vite', author: '老王', count: 1, time: "2022-08-13", imageList: [] },
                { id: 10, title: 'vite', author: '老王', count: 1, time: "2022-08-13", imageList: [] },
            ],//列表数据
            timestamp: null,//获取下一页数据的时间戳

            loading: false,//控制加载loading状态
            finished: false,//加载结束
            error: false,//失败提示

            refreshingLoading: false,//下拉刷新loading状态
            refreshingText: '刷新成功',//刷新成功提示文本
        }
    },
    created() {

    },
    methods: {
        // 初始化或上拉加载
        async onLoad() {
            try {
                // 1、请求获取数据
                // const { res } = await getArticleList({
                //     id: this.channel.id,
                //     timestamp:this.timestamp||Date.now(),
                //     with_top:1,
                // })

                // 模拟请求失败
                // if (Math.random()>0.5) {
                //     JSON.parse('dsna1usia')
                // }

                // 2、放到list里
                // const {result}=res.data.data
                // 数组展开操作符
                // this.list.push(...result)

                // 3、加载结束后=》状态为结束
                // this.loading = false

                // 4、判断是否全部加载完成
                // if (result.length) {
                //     this.timestamp=res.data.pre_timestamp
                // } else {
                //     // 没有数据了
                //     this.finished = true
                // }
            } catch (err) {
                // this.error = true
                // this.loading = false
            }
        },
        // 下拉加载
        async onRefresh() {
            try {
                // 1、请求获取数据=>列表顶部
                // const { res } = await getArticleList({
                //     id: this.channel.id,
                //     timestamp:Date.now(),//下拉刷新获取最新数据，所以传递当前最新时间戳
                //     with_top:1,
                // })
                // this.list.unshift(...res.data.data)

                // 2、关闭loading
                // this.refreshingLoading = false

                // 更新下拉刷新提示文本
                // this.refreshingText = `刷新成功，更新了${res.data.data.length}条数据`
            } catch (err) {
                // this.refreshingText = '刷新失败'
                // this.refreshingLoading = false
            }
        },
    },
}
</script>

<style scoped lang="less">
.articleList {
    height: 79vh;
    overflow-y: auto;
}
</style>